// src/services/theaterApi.ts
import axios from 'axios'

// API配置
const API_BASE_URL = 'http://101.126.81.59:8080' // 你的FastAPI服务地址

// 创建axios实例
const apiClient = axios.create({
  baseURL: API_BASE_URL,
  timeout: 60000, // 60秒超时
  headers: {
    'Content-Type': 'application/json',
  },
})

// 响应拦截器
apiClient.interceptors.response.use(
  (response) => response,
  (error) => {
    console.error('API请求错误:', error)
    throw error
  },
)

// 请求和响应类型定义
export interface ApiResponse<T = any> {
  success: boolean
  message: string
  data?: T
  error_code?: string
}

export interface GenerateTheaterRequest {
  title: string
  num_nodes?: number
  num_characters?: number
  num_skills?: number
}

export interface GenerateComponentRequest {
  title: string
  theater_type?: string
  context?: Record<string, any>
}

// API服务类
export class TheaterApiService {
  // 健康检查
  static async healthCheck(): Promise<ApiResponse> {
    const response = await apiClient.get('/health')
    return response.data
  }

  // 生成完整剧场数据
  static async generateCompleteTheater(request: GenerateTheaterRequest): Promise<ApiResponse> {
    const response = await apiClient.post('/generate/complete', request)
    return response.data
  }

  // 仅生成剧场基本信息
  static async generateTheaterData(request: GenerateComponentRequest): Promise<ApiResponse> {
    const response = await apiClient.post('/generate/theater-data', request)
    return response.data
  }

  // 仅生成剧情节点
  static async generateScriptNodes(request: GenerateComponentRequest): Promise<ApiResponse> {
    const response = await apiClient.post('/generate/script-nodes', request)
    return response.data
  }

  // 仅生成角色
  static async generateCharacters(request: GenerateComponentRequest): Promise<ApiResponse> {
    const response = await apiClient.post('/generate/characters', request)
    return response.data
  }

  // 仅生成技能池
  static async generateSkills(request: GenerateComponentRequest): Promise<ApiResponse> {
    const response = await apiClient.post('/generate/skills', request)
    return response.data
  }

  // 转换为Vue格式
  static async convertToVueFormat(theaterData: any): Promise<ApiResponse> {
    const response = await apiClient.post('/convert/vue', theaterData)
    return response.data
  }
}
